<?php

namespace App\Service\Third;

use App\Job\ProcessOnlineStatusCallback;
use App\Service\BaseService;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\Utils\ApplicationContext;

/**
 * 融云在线状态回调
 */
class RongOnlineStatusService extends BaseService
{
    function __construct(DriverFactory $driverFactory)
    {
        parent::__construct($driverFactory);
        $this->driverFactory = $driverFactory;
        // $this->queueDriver = $driverFactory->get('default');
        // $this->jikeQueueDriver = $driverFactory->get('default');
    }

    public function handle()
    {
        //处理回调信息
        $this->doProcess();
        return $this->responseJson();
    }

    /**
     * 处理回调信息
     * @param array $callbackInfo
     */
    function doProcess()
    {
        $callbackInfo = $this->request->all();
        $userId = $callbackInfo[0]['userid'];
        $prefix = explode('_', $userId)[0];
        switch ($prefix) {
            case env('RONG_PREFIX_JIKELIAO', 'jikeliao'):
                $queueKey = 'jikeliao';
                break;
            case env('RONG_PREFIX_MIAI', 'miai'):
                $queueKey = 'miai';
                break;
            default:
                $queueKey = 'default';
                break;
        }
        log_info(__CLASS__ . "==online==" . json_encode($callbackInfo) . "==Key:{$queueKey}");
        $queDriver = ApplicationContext::getContainer()->get(DriverFactory::class)->get($queueKey);
        $queDriver->push(new ProcessOnlineStatusCallback($callbackInfo));
    }

    /**
     * 返回制定信息
     */
    function responseJson($statusCode = 0)
    {
        $ret = [
            "ActionStatus" => "OK",
            "ErrorInfo" => "",
            "ErrorCode" => $statusCode
        ];
        return $ret;
    }
}
